package com.reliable.service.serviceimpl;

import com.reliable.api.dto.chatmessage.ChatMessageRowForIp;
import com.reliable.service.domain.ReliableSessionIpDetail;
import com.reliable.service.domain.ReliableSessionIpOccurrences;
import com.reliable.service.mapper.ReliableSessionIpOccurrencesMapper;
import com.reliable.service.service.IReliableSessionIpOccurrencesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;

/**
 * 会话中IP出现记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-11-10
 */
@Service
public class ReliableSessionIpOccurrencesServiceImpl extends ServiceImpl<ReliableSessionIpOccurrencesMapper, ReliableSessionIpOccurrences> implements IReliableSessionIpOccurrencesService {

    @Autowired
    private ReliableSessionIpOccurrencesMapper sessionIpMapper;

    @Override
    public void processNewMessages(List<ChatMessageRowForIp> messages) {
        for (ChatMessageRowForIp msg : messages) {
            if (msg.getVisitorIp() == null) continue;
            ReliableSessionIpOccurrences record = new ReliableSessionIpOccurrences();
            record.setSessionId(msg.getSessionId());
            record.setIp(msg.getVisitorIp());
            record.setFirstSeenAt(msg.getCreatedAt());
            record.setLastSeenAt(msg.getCreatedAt());
            sessionIpMapper.upsert(record);
        }
    }

    @Override
    public List<ReliableSessionIpDetail> selectSessionIpDetailList(String sessionId, String ip, String city, String startTime, String endTime, int pageNum, int pageSize) {
        // 计算OFFSET值
        int offset = (pageNum - 1) * pageSize;
        return sessionIpMapper.selectSessionIpDetailList(sessionId, ip, city, startTime, endTime, offset, pageSize);
    }
}